/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerConfigGUI.java
 *
 * Created on Sep 22, 2009, 8:59:36 PM
 */
package fxbattle.server;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;

/**
 *
 * @author curtis
 */
public class ServerConfigGUI extends JFrame {

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JLabel              fTitleLabel;
    private JSeparator          fTitleSeparator;

    private JLabel              fWidthLabel;
    private JFormattedTextField fWidthField;
    
    private JLabel              fHeightLabel;
    private JFormattedTextField fHeightField;
    
    private JLabel              fNumPlayersLabel;
    private JFormattedTextField fNumPlayersField;
    
    private JLabel              fNumAILabel;
    private JFormattedTextField fNumAIField;

    private JLabel              fGenerationRateLabel;
    private JFormattedTextField fGenerationRateField;

    private JSeparator          fButtonSeparator;

    private JButton             fCancelButton;
    private JButton             fStartButton;
    // End of variables declaration

    /** Creates new form ServerConfigGUI */
    public ServerConfigGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        
        LayoutManager layout = new GridLayout(0, 2);
        
        fTitleLabel          = new javax.swing.JLabel();
        fTitleSeparator      = new javax.swing.JSeparator();
        fWidthLabel          = new javax.swing.JLabel();
        fWidthField          = new javax.swing.JFormattedTextField();
        fHeightLabel         = new javax.swing.JLabel();
        fHeightField         = new javax.swing.JFormattedTextField();
        fNumPlayersLabel     = new javax.swing.JLabel();
        fNumPlayersField     = new javax.swing.JFormattedTextField();
        fNumAILabel          = new javax.swing.JLabel();
        fNumAIField          = new javax.swing.JFormattedTextField();
        fGenerationRateLabel = new javax.swing.JLabel();
        fGenerationRateField = new javax.swing.JFormattedTextField();
        fButtonSeparator     = new javax.swing.JSeparator();
        fCancelButton        = new javax.swing.JButton();
        fStartButton         = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FxBattle Game Config");
        setAlwaysOnTop(true);
        setResizable(true);
        setLayout(layout);

        fTitleLabel.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        fTitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        fTitleLabel.setText("FxBattle Game Configuration");

        fWidthLabel.setLabelFor(fWidthField);
        fWidthLabel.setText("Width");

        fWidthField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        fWidthField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fWidthFieldPropertyChange(evt);
            }
        });

        fHeightLabel.setLabelFor(fHeightField);
        fHeightLabel.setText("Height");

        fHeightField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        fHeightField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fHeightFieldPropertyChange(evt);
            }
        });

        fNumPlayersLabel.setLabelFor(fNumPlayersField);
        fNumPlayersLabel.setText("Number of Players");

        fNumPlayersField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        fNumPlayersField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fNumPlayersFieldPropertyChange(evt);
            }
        });

        fNumAILabel.setLabelFor(fNumAIField);
        fNumAILabel.setText("Number of AI");

        fNumAIField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        fNumAIField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fNumAIFieldPropertyChange(evt);
            }
        });

        fGenerationRateLabel.setLabelFor(fGenerationRateField);
        fGenerationRateLabel.setText("Generation Rate");

        fGenerationRateField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        fGenerationRateField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fGenerationRateFieldPropertyChange(evt);
            }
        });

        fStartButton.setText("Start");
        fStartButton.setEnabled(false);
        fStartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fStartButtonActionPerformed(evt);
            }
        });

        fCancelButton.setText("Cancel");
        fCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fCancelButtonActionPerformed(evt);
            }
        });

        add(fWidthLabel);
        add(fWidthField);
        add(fHeightLabel);
        add(fHeightField);
        add(fNumPlayersLabel);
        add(fNumPlayersField);
        add(fNumAILabel);
        add(fNumAIField);
        add(fGenerationRateLabel);
        add(fGenerationRateField);
        add(fCancelButton);
        add(fStartButton);
        
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private int fWidth = 0;
    private int fHeight = 0;
    private int fPlayers = 0;
    private int fAI = 0;
    private int fGenRate = 0;

    private int fServerPort = 0;

    public void setServerPort (int iServerPort) {
        fServerPort = iServerPort;
    }

    private void fStartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fStartButtonActionPerformed
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                gameserver fGame = new gameserver();
                fGame.main(new String[]{
                            String.valueOf(fServerPort),
                            String.valueOf(fHeight),
                            String.valueOf(fWidth),
                            String.valueOf(fPlayers),
                            String.valueOf(fAI),
                            String.valueOf(fGenRate)});
            }
        });
        dispose();
    }//GEN-LAST:event_fStartButtonActionPerformed

    private void fCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fCancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_fCancelButtonActionPerformed

    private void fWidthFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fWidthFieldPropertyChange
        try {
            fWidth = Integer.parseInt(fWidthField.getText());
            inputChanged();
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_fWidthFieldPropertyChange

    private void fHeightFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fHeightFieldPropertyChange
        try {
            fHeight = Integer.parseInt(fHeightField.getText());
            inputChanged();
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_fHeightFieldPropertyChange

    private void fNumPlayersFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fNumPlayersFieldPropertyChange
        try {
            fPlayers = Integer.parseInt(fNumPlayersField.getText());
            inputChanged();
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_fNumPlayersFieldPropertyChange

    private void fNumAIFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fNumAIFieldPropertyChange
         try {
            fAI = Integer.parseInt(fNumAIField.getText());
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_fNumAIFieldPropertyChange

    private void fGenerationRateFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fNumAIFieldPropertyChange
         try {
            fGenRate = Integer.parseInt(fGenerationRateField.getText());
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_fNumAIFieldPropertyChange

    private void inputChanged() {
        fStartButton.setEnabled(fWidth > 0 && fHeight > 0 && fPlayers > 0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ServerConfigGUI().setVisible(true);
            }
        });
    }

}
